var parentData, parentActive;
var dateFormatStr = 'yyyy-MM-dd HH:mm:ss';
layui.use(['form', 'table', 'layer', 'laydate'], function () {
	var form = layui.form,
		layer = layui.layer,
		table = layui.table,
		laydate = layui.laydate,
		$ = layui.$;

	var imgArtwork, imgUrl ,imgSource;
	window.active = {
		reload: () => {
			parentActive.reload();
		},
		addImg: () => {
				var target_num = $("#Images li").length;
				// console.log(target_num);
				if(target_num==6){
					top.layer.confirm('最多添加6张图片', {
						icon: 3,
						title: '提示信息'
					});
				}else{
					imgPop = top.layer.open({
						title: '新增详情图片',
						type: 2,
						area: ["920px", "550px"],
						fixed: false, //不固定
						offset: '50px',
						maxmin: false,
						content: 'page/goods/addImg.html',
						success: async function (layero, index) {
							let cw = layero.find('iframe')[0].contentWindow;
							cw.parentActive = active; //赋予父窗体事件
							setTimeout(function () {
								top.layer.tips('点击此处返回', '.layui-layer-setwin .layui-layer-close', {
									tips: 3
								});
							}, 500);
						}
					});
				}
					
				
		},
		saveImg: (imgurl) => {
			top.layer.close(imgPop);
			$('#Images').html($('#Images').html() +
				`<li><div class="uploadImgDiv"><img layer-src="${imgurl}" src="${imgurl}"></div><div class="operate"><i class="layui-icon img_del">&#xe640;</i></div></li>`);
		}
	};
	//操作
	$('.layui-btn').on('click', function () {
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});
	//表格头部按钮操作
	table.on('toolbar(tableList)', function (obj) {
		var type = obj.event;
		active[type] ? active[type].call(this, obj.data) : '';
	});
	//表格按钮操作
	table.on('tool(tableList)', function (obj) {
		var type = obj.event;
		active[type] ? active[type].call(this, obj.data) : '';
	});

	form.verify({});

	form.on("submit(save)", element => {
		// let data = form.val("dataform");
		// let imgList = $('#Images').find('img');
		
		(async function () {
			let data = form.val("dataform");
			let imgList = $('#Images').find('img');
			data.img ="";
			for (var i = 0; i < imgList.length; i++) {
				data.img += imgList[i].src+ ';';
			}
			data.dt = $('#dt').val();
			let res= await ajaxAsync('auction', data, "PUT")
			if(data.id==''){
				data.id=res
				popEdit(data, 'page/bid/editPrize.html', ["770px", "580px"]) 
			}else{
				popEdit(data, 'page/bid/editPrize.html', ["770px", "580px"])
			}
			parentActive.reload(); //刷新父页面
			if(___popLayerIndex)
				top.layer.close(___popLayerIndex);
			else
				top.layer.closeAll('iframe'); //关闭 
			
		})();
		return false;
	});
	//延时初始化，确保数据已经塞进来了
	setTimeout(() => {
		// console.log(parentData);
		//执行一个laydate实例
		laydate.render({
			elem: '#dt', //指定元素
			type: 'datetime',
			trigger: 'click',
			// min: js.format_datetime(new Date(), dateFormatStr),
			value: js.format_datetime(parentData && parentData.dt || new Date(), dateFormatStr),
		});
		if (!parentData) return;
		//初始化数据
		form.val("dataform", JSON.parse(JSON.stringify(parentData)));
			//初始化图片
			var imgList = [],
				imgSource = parentData.img.split(';')
			for (var i = 0; i < imgSource.length; i++) {
				if (imgSource[i])
					imgList.push(`<li><div class="uploadImgDiv"><img layer-src="${imgSource[i]}" src="${imgSource[i]}"></div><div class="operate"><i class="layui-icon img_del">&#xe640;</i></div></li>`);
			}
			$('#Images').html(imgList.toString());
	}, 200);

	//删除单张图片
	$("body").on("click", ".img_del", function () {
		var _this = $(this);
		top.layer.confirm('确定删除图片吗？', {
			icon: 3,
			title: '提示信息'
		}, function (index) {
			_this.parents("li").hide(1000);
			setTimeout(function () {
				_this.parents("li").remove();
			}, 950);
			top.layer.close(index);
		});
	});
});